package com.beyond.core.codec;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.Checksum;

import com.beyond.core.common.EscapeTool;

/**
 * 木牛雷达协议设置功能改
 * 描述：雷达协议设置待改进版
 * @author shulei
 * @date 2021年7月19日
 *
 */
public class SettingRadarProtocol {

    /**
     * 封装完整的雷达协议
     * @param Msgbody
     * @return
     * @throws IOException
     */
    public static byte[] setNetRadarMsg(byte[] Msgbody) throws IOException {

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ByteArrayOutputStream bosfinal = new ByteArrayOutputStream();
        if (Msgbody != null) {
            bos.write(Msgbody);
        }

        byte[] turn = bos.toByteArray();
        //TODO 考虑校验位
        int checksum = (int) getCRC32Checksum(turn);
        bos.write((byte) (checksum & 0x000000ff));
        bos.close();
        bosfinal.write((byte) 0x7e); //一个数字标识占位 整数129 16进制是0x7e
        bosfinal.write(EscapeTool.escapeData(turn)); //TODO 调用转义处理
        bosfinal.write((byte) 0x7e);
        byte[] returndata = bosfinal.toByteArray();
        bosfinal.close();
        return returndata;
    }
    /**
     * 发送下行指令
     * @param opType,objectFlag,data
     * @return
     * @throws IOException
     */
    public static byte[] sendCommand(byte opType,byte objectFlag,byte[] data) throws IOException {

        ByteArrayOutputStream bos = new ByteArrayOutputStream();// 消息头+消息体
        /********开始拼装消息头********/

        byte linkedAddr = 0 & 0xff;
        byte protocolversion = 0x10;  //协议版本号

        byte[] Msgheader = new byte[4];
        Msgheader[0] = linkedAddr;
        Msgheader[1] = protocolversion;
        Msgheader[2] = opType;
        Msgheader[3] = objectFlag;
        bos.write(Msgheader);
        bos.write(data);
        byte[] body = bos.toByteArray();
        bos.close();
        return setNetRadarMsg(body);
    }

    /**
     * 发送下行指令重载,无消息内容
     * @param opType,objectFlag
     * @return
     * @throws IOException
     */
    public static byte[] sendCommand(byte opType,byte objectFlag) throws IOException {

        ByteArrayOutputStream bos = new ByteArrayOutputStream();// 消息头+消息体
        /********开始拼装消息头********/

        byte linkedAddr = 0 & 0xff;
        byte protocolversion = 0x10;  //协议版本号

        byte[] Msgheader = new byte[4];
        Msgheader[0] = linkedAddr;
        Msgheader[1] = protocolversion;
        Msgheader[2] = opType;
        Msgheader[3] = objectFlag;
        bos.write(Msgheader);
        byte[] body = bos.toByteArray();
        bos.close();
        return setNetRadarMsg(body);
    }
    /**
     * 计算校验和
     * @param bytes
     * @return
     */
    public static long getCRC32Checksum(byte[] bytes) {
        Checksum crc32 = new CRC32();
        crc32.update(bytes, 0, bytes.length);
        return crc32.getValue();
    }

    public static byte[] long2byte(long res) {
        byte[] buffer = new byte[8];
        for (int i = 0; i < 8; i++) {
            int offset = 64 - (i + 1) * 8;
            buffer[i] = (byte) ((res >> offset) & 0xff);
        }
        return buffer;
    }

    public static long byteArrayToLong(byte[] b){
        long values = 0;
        for (int i = 0; i < 8; i++) {
            values <<= 8; values|= (b[i] & 0xff);
        }
        return values;
    }

}
